package com.tangqi.android;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class FundYiActivity extends Activity {
	public static final int BASE_INCOME = 3500;
	
	public static final double TOTAL_FUND_RATIO = 0.31;
	
	public static final double FUND1_RATIO = 0.08;
	public static final double FUND2_RATIO = 0.03;
	public static final double FUND3_RATIO = 0.15;
	public static final double FUND4_RATIO = 0.02;
	public static final double FUND5_RATIO = 0.01;	
	public static final double FUND6_RATIO = 0.01;
	public static final double FUND7_RATIO = 0.01;

	private static final String TAG = "FundYiActivity";

	private EditText grossIncomeTxt;
	private EditText fundTxt;
	
	private EditText fundTxt1;
	private EditText fundTxt2;
	private EditText fundTxt3;
	private EditText fundTxt4;
	private EditText fundTxt5;
	private EditText fundTxt6;
	private EditText fundTxt7;
	
	private Button calculatorBtn;
	private Button resetBtn;
	
	private EditText deductFundIncomeTxt;
	private EditText incomeTaxTxt;
	private EditText deductTaxIncomeTxt;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.yi);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		
		calculatorBtn = (Button) findViewById(R.id.calculatorBtn);
		calculatorBtn.setOnClickListener(listener);
		resetBtn = (Button) findViewById(R.id.resetBtn);
		resetBtn.setOnClickListener(listener);
		
		grossIncomeTxt = (EditText) findViewById(R.id.grossIncomeTxt);
		fundTxt = (EditText) findViewById(R.id.fundTxt);				
		fundTxt1 = (EditText) findViewById(R.id.fundTxt1);
		fundTxt2 = (EditText) findViewById(R.id.fundTxt2);
		fundTxt3 = (EditText) findViewById(R.id.fundTxt3);
		fundTxt4 = (EditText) findViewById(R.id.fundTxt4);
		fundTxt5 = (EditText) findViewById(R.id.fundTxt5);		
		fundTxt6 = (EditText) findViewById(R.id.fundTxt6);
		fundTxt7 = (EditText) findViewById(R.id.fundTxt7);
		
		deductFundIncomeTxt = (EditText) findViewById(R.id.deductFundIncomeTxt);
		incomeTaxTxt = (EditText) findViewById(R.id.incomeTaxTxt);
		deductTaxIncomeTxt = (EditText) findViewById(R.id.deductTaxIncomeTxt);
    }
    
	private OnClickListener listener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Button btn = (Button) v;
			Log.i(TAG, btn.getId() + " is clicked");
			switch (btn.getId()) {
			case R.id.calculatorBtn:
				calculateAll();
				break;
			case R.id.resetBtn:
				resetAll();
				break;
			}
		}
	};
	
	/**
	 * 
	 */
	public void calculateAll() {
		
		//缴费基数
		double grossIncome = Double.valueOf(grossIncomeTxt.getText().toString());
		
		//判断缴费基数是否超出范围
		String warningMsg = CalUtil.checkBound("YI", grossIncome);
		if(warningMsg!=null){
			new AlertDialog.Builder(this).setMessage(warningMsg).show();
			return;
		}
		
		//公积金总额
		double fund = TOTAL_FUND_RATIO * grossIncome;
		
		double deductFundIncome = 0.89 * grossIncome;
		double incomeTax = CalUtil.calculateIIT(deductFundIncome);
		double deductTaxIncome = deductFundIncome - incomeTax;

		Log.i(TAG, "grossIncome=" + grossIncome);
		Log.i(TAG, "fund=" + fund);
		Log.i(TAG, "deductFundIncome=" + deductFundIncome);
		Log.i(TAG, "incomeTax=" + incomeTax);
		Log.i(TAG, "deductTaxIncome=" + deductTaxIncome);
		
		fundTxt.setText(String.valueOf(CalUtil.tranFormat(fund)));	
		fundTxt1.setText(String.valueOf(CalUtil.tranFormat(grossIncome * FUND1_RATIO)));
		fundTxt2.setText(String.valueOf(CalUtil.tranFormat(grossIncome * FUND2_RATIO)));
		fundTxt3.setText(String.valueOf(CalUtil.tranFormat(grossIncome * FUND3_RATIO)));
		fundTxt4.setText(String.valueOf(CalUtil.tranFormat(grossIncome * FUND4_RATIO)));
		fundTxt5.setText(String.valueOf(CalUtil.tranFormat(grossIncome * FUND5_RATIO)));		
		fundTxt6.setText(String.valueOf(CalUtil.tranFormat(grossIncome * FUND6_RATIO)));
		fundTxt7.setText(String.valueOf(CalUtil.tranFormat(grossIncome * FUND7_RATIO)));
		
		deductFundIncomeTxt.setText(String.valueOf(CalUtil.tranFormat(deductFundIncome)));
		incomeTaxTxt.setText(String.valueOf(CalUtil.tranFormat(incomeTax)));
		deductTaxIncomeTxt.setText(String.valueOf(CalUtil.tranFormat(deductTaxIncome)));

	}

	/**
	 * 
	 */
	public void resetAll() {
		grossIncomeTxt.setText(null);
		fundTxt.setText(null);
		deductFundIncomeTxt.setText(null);
		incomeTaxTxt.setText(null);
		deductTaxIncomeTxt.setText(null);
		
		fundTxt.setText(null);
		fundTxt1.setText(null);
		fundTxt2.setText(null);
		fundTxt3.setText(null);
		fundTxt4.setText(null);
		fundTxt5.setText(null);
		fundTxt6.setText(null);
		fundTxt7.setText(null);
	}
}
